﻿<script type="text/javascript" src="${base}/scripts/base/stringOperate.js"></script>
<script type="text/javascript">
$(function () {
    $("[name='bgcgdJhdwId']").ljsxyCombobox({lx: 3, sxy: "xy", jyjg: $("[name='bgcgdZgsId']").val()});
    $("[name='bgcgdGys']").qyjgzcombobox();
    $("[name='bgwzdl']").yjwzbmCombobox();
    var selectedGrid = $("#search-bgcgd-selected-bgcgd-grid").edatagrid({
        fitColumns: false,
        rownumbers: true,
        singleSelect: true,
        pageNumber: 1,
        fit: true,
        frozenColumns: [
            [
                {field: 'ggcode', title: '老物资编码', width: 100},
                {field: 'ggname', title: '物资名称', width: 100},
                {field: 'ggxh', title: '规格型号', width: 70},
                {field: 'ggdw', title: '单位', width: 30},
                {field: 'jhsl', title: '计划数量', width: 50},
                {field: 'wcgsl', title: '未采购', width: 50},
                {field: 'cgfs', title: '采购方式', width: 70, formatter: function (value) {
                    if (value == 1) {
                        return "比价";
                    } else if (value == 2) {
                        return "招标"
                    }
                }}
            ]
        ],
        columns: [
            [

//                    {field:'ggname', title:'可开单(数量)', width:70},
//                    {field:'htbm', title:'可开单(万元)', width:70},
                {field: 'htbm', title: '合同号', width: 130},
                {field: 'htqdr', title: '合同签订人', width: 70},
                {field: 'htdrsj', title: '合同导入时间', width: 80},
                {field: 'gysmc', title: '供应商', width: 150},
                {field: 'dhrq', title: '到货日期', width: 70},
                {field: 'jhsj', title: '计划日期', width: 70},
                {field: 'jhyxm', title: '计划员', width: 70},
                {field: 'bz', title: '备注', width: 150},
                {field: 'hzsm', title: '计划描述', width: 350},
                {field: 'ggth', title: '图号', width: 70}
            ]
        ],
        onSelect: function (rowIndex, rowData) {
            //获取每个单元格
            $("#search-bgcgd-selected-bgcgd-grid").edatagrid('editRow', rowIndex);
            var editors = $("#search-bgcgd-selected-bgcgd-grid").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
        },
        onRowContextMenu: function (e, rowIndex, rowData) {
            e.preventDefault();
            $('#search-bgcgd-menu').menu('show', {
                left: e.pageX,
                top: e.pageY
            }).data("rowIndex", rowIndex);
        }
    });

    $("#bgcgd_into_grid").edatagrid({
        url: $.webPath() + "/cggl/bgcgdgl/bgcgdSearchList",
        fitColumns: false,
        singleSelect: true,
        toolbar: "#toolbar_bgcgd_search",
        fit: true,
        pagination: true,
        checkOnSelect: false,
        selectOnCheck: false,
        pageNumber: 1,
        rownumbers: true,
        frozenColumns: [
            [
                {field: 'ggcode', title: '老物资编码', width: 100},
                {field: 'ggname', title: '物资名称', width: 100},
                {field: 'ggxh', title: '规格型号', width: 70},
                {field: 'ggdw', title: '单位', width: 30},
                {field: 'jhsl', title: '计划数量', width: 50},
                {field: 'wcgsl', title: '未采购', width: 50},
                {field: 'cgfs', title: '采购方式', width: 70, formatter: function (value) {
                    if (value == 1) {
                        return "比价";
                    } else if (value == 2) {
                        return "招标"
                    }
                }}
            ]
        ],
        columns: [
            [

//                    {field:'ggname', title:'可开单(数量)', width:70},
//                    {field:'htbm', title:'可开单(万元)', width:70},
                {field: 'htbm', title: '合同号', width: 130},
                {field: 'htqdr', title: '合同签订人', width: 70},
                {field: 'htdrsj', title: '合同导入时间', width: 80},
                {field: 'gysmc', title: '供应商', width: 150},
                {field: 'dhrq', title: '到货日期', width: 70},
                {field: 'jhsj', title: '计划日期', width: 70},
                {field: 'jhyxm', title: '计划员', width: 70},
                {field: 'bz', title: '备注', width: 150},
                {field: 'hzsm', title: '计划描述', width: 350},
                {field: 'ggth', title: '图号', width: 70}
            ]
        ],
        onSelect: function (rowIndex, rowData) {
            var data = $('#search-bgcgd-selected-bgcgd-grid').datagrid("getData");
            if (data && data.rows.length != 0) {
                $("#htId").val(data.rows[0].htid);
                $("#ckId").val(data.rows[0].dhck_id);
                if (rowData.htid != data.rows[0].htid) {
                    $.messager.alert('提示', "选择的物资与已选择的物资不在同一个合同!请重新选择", 'info');
                } else {
                    var flag1 = true, flag2 = true;
                    $.each(data.rows, function (i, val) {
                        if (val.hzid != rowData.hzid) {
                            flag1 = false;
                            return false;
                        } else if (val.ggcode == rowData.ggcode) {
                            flag2 = false;
                            return false;
                        }
                    })
                    if (flag1 & flag2) {
                        $(selectedGrid).datagrid('appendRow', rowData);
                    } else if (!flag1) {
                        $.messager.alert('提示', "选择的物资与已选择的物资不在同一个计划!请重新选择", 'info');
                    } else if (!flag2) {
                        $.messager.alert('提示', "选择的物资与已选过!请重新选择", 'info');
                    }
                }
            } else {
                if (!rowData.htid) {
                    $.messager.alert('提示', "此物资未关联合同!请重新选择", 'info');
                } else {
                    $("#htId").val(rowData.htid);
                    $("#ckId").val(rowData.dhck_id);
                    $(selectedGrid).datagrid('appendRow', rowData);
                }
            }

            var wzggIds = sumDataPropertyToString(data, 'ggid');
            var hzmxIds = sumDataPropertyToString(data, 'hzmxid');
            var htmxIds = sumDataPropertyToString(data, 'htmxid');
            var jhsls = sumDataPropertyToString(data, 'wcgsl');
            $("#wzggIds").val(wzggIds);
            $("#htmxIds").val(htmxIds);
            $("#hzmxIds").val(hzmxIds);
            $("#jhsls").val(jhsls);
        }
    });

    $("#search-bgcgd-menu").menu({
        onClick: function (item) {
            if (item.name == "delete") {
                var data = $("#search-bgcgd-selected-bgcgd-grid").datagrid('getData');
                var row = data.rows[$(this).data("rowIndex")];
                $("#wzggIds").val(StringOperate.remove($("#wzggIds").val(), row.ggid));
                $("#htmxIds").val(StringOperate.remove($("#hzmxIds").val(), row.htmxid));
                $("#hzmxIds").val(StringOperate.remove($("#hzmxIds").val(), row.hzmxid));
                $("#jhsls").val(StringOperate.remove($("#jhsls").val(), row.wcgsl));
                $("#search-bgcgd-selected-bgcgd-grid").datagrid('deleteRow', $(this).data("rowIndex"));
            }
        }
    });

    $("#btn_bgcgd_search_init").click(function () {
        $('#bgcgd_into_grid').edatagrid({
            url: $.webPath() + "/cggl/bgcgdgl/bgcgdSearchList",
            pageNumber: 1,
            queryParams: {
                wzbm: $('#bgcgd_code').val(),
                wzdl: $("[name='bgwzdl']").val(),
                jhdwId: $("[name='bgcgdJhdwId']").val(),
                startDate: $("[name='bgcgdStartDate']").val(),
                endDate: $("[name='bgcgdEndDate']").val(),
                htbm: $("[name='bgcgdHtbm']").val(),
                gysid: $("[name='bgcgdGys']").val()
            },
            frozenColumns: [
                [
                    {field: 'ggcode', title: '老物资编码', width: 100},
                    {field: 'ggname', title: '物资名称', width: 100},
                    {field: 'ggxh', title: '规格型号', width: 70},
                    {field: 'ggdw', title: '单位', width: 30},
                    {field: 'jhsl', title: '计划数量', width: 50},
                    {field: 'wcgsl', title: '未采购', width: 50},
                    {field: 'cgfs', title: '采购方式', width: 70, formatter: function (value) {
                        if (value == 1) {
                            return "比价";
                        } else if (value == 2) {
                            return "招标"
                        }
                    }}
                ]
            ],
            columns: [
                [

//                    {field:'ggname', title:'可开单(数量)', width:70},
//                    {field:'htbm', title:'可开单(万元)', width:70},
                    {field: 'htbm', title: '合同号', width: 130},
                    {field: 'htqdr', title: '合同签订人', width: 70},
                    {field: 'htdrsj', title: '合同导入时间', width: 80},
                    {field: 'gysmc', title: '供应商', width: 150},
                    {field: 'dhrq', title: '到货日期', width: 70},
                    {field: 'jhsj', title: '计划日期', width: 70},
                    {field: 'jhyxm', title: '计划员', width: 70},
                    {field: 'bz', title: '备注', width: 150},
                    {field: 'hzsm', title: '计划描述', width: 350},
                    {field: 'ggth', title: '图号', width: 70}
                ]
            ],
            onSelect: function (rowIndex, rowData) {
                var data = $('#search-bgcgd-selected-bgcgd-grid').datagrid("getData");
                if (data && data.rows.length != 0) {
                    $("#htId").val(data.rows[0].htid);
                    $("#ckId").val(data.rows[0].dhck_id);

                    if (rowData.htid != data.rows[0].htid) {
                        $.messager.alert('提示', "选择的物资与已选择的物资不在同一个合同!请重新选择", 'info');
                    } else {
                        var flag1 = true, flag2 = true;
                        $.each(data.rows, function (i, val) {
                            if (val.hzid != rowData.hzid) {
                                flag1 = false;
                                return false;
                            } else if (val.ggcode == rowData.ggcode) {
                                flag2 = false;
                                return false;
                            }
                        })
                        if (flag1 & flag2) {
                            $(selectedGrid).datagrid('appendRow', rowData);
                        } else if (!flag1) {
                            $.messager.alert('提示', "选择的物资与已选择的物资不在同一个计划!请重新选择", 'info');
                        } else if (!flag2) {
                            $.messager.alert('提示', "选择的物资与已选过!请重新选择", 'info');
                        }
                    }
                } else {
                    if (!rowData.htid) {
                        $.messager.alert('提示', "此物资未关联合同!请重新选择", 'info');
                    } else {
                        $("#htId").val(rowData.htid);
                        $("#ckId").val(rowData.dhck_id);
                        $(selectedGrid).datagrid('appendRow', rowData);
                    }

                }
                var wzggIds = sumDataPropertyToString(data, 'ggid');
                var htmxIds = sumDataPropertyToString(data, 'htmxid');
                var hzmxIds = sumDataPropertyToString(data, 'hzmxid');
                var jhsls = sumDataPropertyToString(data, 'wcgsl');
                $("#wzggIds").val(wzggIds);
                $("#htmxIds").val(htmxIds);
                $("#hzmxIds").val(hzmxIds);
                $("#jhsls").val(jhsls);
            }
        });
    });

})

function sumDataPropertyToString(data, sx) {
    var property = "";
    for (var i = 0; i < data.rows.length; i++) {
        property += (data.rows[i][sx] + ",");
    }
    return property.substring(0, property.length - 1);
}

</script>
<div class="easyui-layout" data-options="fit:true">
    <input type="hidden" id="wzggIds"/>
    <input type="hidden" id="htId"/>
    <input type="hidden" id="ckId"/>
    <input type="hidden" id="htmxIds"/>
    <input type="hidden" id="hzmxIds"/>
    <input type="hidden" id="jhsls"/>

    <div data-options="region:'north',split:true" style="height:375px">
        <div id="toolbar_bgcgd_search" style="height:auto">
            <div class="textcon">
                <table class="tb" style="margin-left: 12px;">
                    <tr>
                        <th>物资编号:</th>
                        <td>
                            <input id="bgcgd_code" name="code" type="text" class="input-text">
                        </td>
                        <th>物资大类：</th>
                        <td>
                            <select class="easyui-combobox" name="bgwzdl" style="width:165px;" panelHeight='130'>
                            </select>
                        </td>
                        <th>计划单位：</th>
                        <td>
                            <input name="bgcgdZgsId" type="hidden" value="${(zgs.id)!}"/>
                            <input name="bgcgdJhdwId" type="text" class="easyui-combobox" style="width:165px;"/>
                        </td>
                    </tr>
                    <tr>
                        <th>计划日期：</th>
                        <td><input name="bgcgdStartDate" type="text" class="easyui-datebox" style="width:165px"
                                   value="${(sysdate?date)!}"/></td>
                        <td align="center">至</td>
                        <td><input name="bgcgdEndDate" type="text" class="easyui-datebox" style="width:165px"
                                   value="${(datetime?date)!}"/></td>
                        <th>合同编号：</th>
                        <td><input name="bgcgdHtbm" type="text" class="input-text"
                                   value="${(htbm)!}"/></td>
                    </tr>
                    <tr>
                        <th>供应商：</th>
                        <td><input name="bgcgdGys" type="text" class="input-text" value=""/></td>
                        <td style=" padding-left:5px;">
                            <input id="btn_bgcgd_search_init" type="button" value=""
                                   class="submit"/>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <table id="bgcgd_into_grid"></table>
    </div>
    <div data-options="region:'center'">
        <table id="search-bgcgd-selected-bgcgd-grid">
        </table>
        <div id="search-bgcgd-menu" class="easyui-menu" style="width:120px;">
            <div name="delete">删除</div>
        </div>
    </div>
</div>